﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace NFinal
{
    public struct ReWriteData
    {
        public List<NFinal.Compile.RewriteDirectory> rewriteDirectoryList;
        public List<NFinal.Compile.RewriteFile> rewriteFileList;
    }
    public class CompileConfigManager
    {
        public static CompileConfig GetConfig(string appName, string root)
        {
            string configFileName = Frame.MapPath(appName + "/config.json");
            return GetConfig(appName, configFileName, root);
        }
        //配置初始化
        public static CompileConfig GetConfig(string appName, string configFileName, string root)
        {
            CompileConfig config = new CompileConfig();
            if (File.Exists(configFileName))
            {
                StreamReader sr = new StreamReader(configFileName, System.Text.Encoding.UTF8);
                string json = sr.ReadToEnd();
                sr.Dispose();
                LitJson.JsonData configData = LitJson.JsonMapper.ToObject(json);
                config.defaultStyle = configData["defaultStyle"].ToString();
                int.TryParse(configData["urlMode"].ToString(), out config.UrlMode);
                config.connectionStrings = new System.Collections.Generic.List<DB.ConnectionString>();
                LitJson.JsonData connectionStringArray = configData["connectionStrings"];
                bool.TryParse(configData["compressHTML"].ToString(), out config.CompressHTML);
                config.cookiePrefix = configData["cookiePrefix"].ToString();
                config.sessionPrefix = configData["sessionPrefix"].ToString();
                config.urlPrefix = configData["urlPrefix"].ToString();
                config.urlExtension = configData["urlExtension"].ToString();
                NFinal.DB.ConnectionString conStr = null;

                for (int i = 0; i < connectionStringArray.Count; i++)
                {
                    conStr = new DB.ConnectionString();
                    conStr.name = connectionStringArray[i]["name"].ToString();
                    conStr.provider = connectionStringArray[i]["providerName"].ToString();
                    conStr.value = connectionStringArray[i]["connectionString"].ToString();
                    //修正conStr.value|ModuleDataDirectory|,|DataDirectory|
                    if (conStr.value.IndexOf("|ModuleDataDirectory|") > -1)
                    {
                        conStr.value = conStr.value.Replace("|ModuleDataDirectory|", root + appName + "\\Data\\");
                    }
                    if (conStr.value.IndexOf("|DataDirectory|") > -1)
                    {
                        conStr.value = conStr.value.Replace("|DataDirectory|", root + "App_Data\\");
                    }
                    config.connectionStrings.Add(conStr);
                }
                LitJson.JsonData rewriteDirectory = configData["rewriteDirectory"];
                NFinal.Compile.RewriteDirectory rd;
                for (int i = 0; i < rewriteDirectory.Count; i++)
                {
                    config.rewriteDirectoryList.Clear();
                    rd = new Compile.RewriteDirectory();
                    rd.from = rewriteDirectory[i]["from"].ToString();
                    rd.to = rewriteDirectory[i]["to"].ToString();
                    config.rewriteDirectoryList.Add(rd);
                }
                LitJson.JsonData rewriteFile = configData["rewriteFile"];
                NFinal.Compile.RewriteFile rf;
                for (int i = 0; i < rewriteFile.Count; i++)
                {
                    config.rewriteFileList.Clear();
                    rf = new Compile.RewriteFile();
                    rf.from = rewriteFile[i]["from"].ToString();
                    rf.to = rewriteFile[i]["to"].ToString();
                    config.rewriteFileList.Add(rf);
                }
                LitJson.JsonData redisConfig = configData["redis"];
                bool.TryParse(redisConfig["autoStart"].ToString(), out config.redisConfigAutoStart);
                int.TryParse(redisConfig["maxReadPoolSize"].ToString(), out config.redisConfigMaxReadPoolSize);
                int.TryParse(redisConfig["maxWritePoolSize"].ToString(), out config.redisConfigMaxWritePoolSize);
                LitJson.JsonData readWriteHosts = redisConfig["readWriteHosts"];
                if (readWriteHosts == null || readWriteHosts.Count < 1)
                {
                    config.redisReadWriteHosts = null;
                }
                else
                {
                    config.redisReadWriteHosts = new string[readWriteHosts.Count];
                }
                for (int i = 0; i < readWriteHosts.Count; i++)
                {
                    config.redisReadWriteHosts[i] = readWriteHosts[i].ToString();
                }
                LitJson.JsonData readOnlyHosts = redisConfig["readOnlyHosts"];
                if (readOnlyHosts == null || readOnlyHosts.Count < 1)
                {
                    config.redisReadOnlyHosts = null;
                }
                else
                {
                    config.redisReadOnlyHosts = new string[readWriteHosts.Count];
                }
                for (int i = 0; i < readOnlyHosts.Count; i++)
                {
                    config.redisReadOnlyHosts[i] = readOnlyHosts[i].ToString();
                }
                //if (NFinal.Cache.clientManager == null)
                //{
                //    ServiceStack.Redis.RedisClientManagerConfig redisClientManagerConfig = new ServiceStack.Redis.RedisClientManagerConfig();
                //    redisClientManagerConfig.AutoStart = config.redisConfigAutoStart;
                //    redisClientManagerConfig.MaxReadPoolSize = config.redisConfigMaxReadPoolSize;
                //    redisClientManagerConfig.MaxWritePoolSize = config.redisConfigMaxWritePoolSize;
                //    NFinal.Cache.clientManager = new ServiceStack.Redis.PooledRedisClientManager(config.redisReadWriteHosts, config.redisReadOnlyHosts, redisClientManagerConfig);
                //}
            }

            if (string.IsNullOrEmpty(appName))
            {
                config.APP = "/";
            }
            else
            {
                config.APP = "/" + appName.Trim(new char[] { '/' }) + "/";
            }
            config.Controller = config.APP + config.Controller;
            config.Code = config.APP + config.Code;
            config.Views = config.APP + config.Views;
            config.Models = config.APP + config.Models;
            config.BLL = config.Models + config.BLL;
            config.DAL = config.Models + config.DAL;
            config.Web = config.APP + config.Web;
            config.Content = config.APP + config.Content;
            config.ContentCss = config.Content + config.ContentCss;
            config.ContentJS = config.Content + config.ContentJS;
            config.ContentImages = config.Content + config.ContentImages;
            return config;
        }
    }
}
